package com.foreknow.controller;

import com.foreknow.model.Admin;
import com.foreknow.service.impl.AdminDaoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "Login.Servlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                response.setContentType("text/html;charset=utf-8");
                request.setCharacterEncoding("utf-8");
        PrintWriter writer = response.getWriter();
        //new AdminServiceimpl的对象
        AdminDaoServiceImpl service = new AdminDaoServiceImpl();
        //将从页面中获取到的两个参数传到方法中
        Admin admin = service.isValidate(request.getParameter("name"), request.getParameter("password"));
        if (admin!=null){
            //判断如果对象不为空则执行下列语句
            //调用请求命令的请求转发的方法,?q=list是一种表达式 这里/manager是ManagerServlet;
            request.getRequestDispatcher("/manager?q=list").forward(request,response);
        }else {
            writer.println("登录失败");
    }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
